#include <QFile>
#include <QTextStream>
#include "file_process.h"

#include "track_process.h"
#include "track_process_02.h"

File_process::File_process(QObject *parent)
    : QObject(parent)
    , data_process_(new Track_process(this))
//    , data_process02_(new Track_process_02(this))
    , thread_flag_(false)
{
    init_connections();
}

File_process::~File_process()
{
    if (nullptr != data_process_) {
        delete data_process_;
        data_process_ = nullptr;
    }
}

void File_process::init_connections()
{
    connect(this, &File_process::si_split_data_from_files,
            this, &File_process::sl_split_data_from_files);

    connect(this, &File_process::si_read_data_from_file,
            this, &File_process::sl_read_data_from_file);
}

void File_process::sl_split_data_from_files(QStringList file_list)
{
    QString file_name_wiht_path;
    foreach(file_name_wiht_path, file_list) {
        QFile file(file_name_wiht_path);
        if ( !file.exists()) {
            continue;
        }

        if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
            QTextStream in(&file);
            // 自动监测Unicode
            in.setAutoDetectUnicode(true);

            // 创建保存拆分数据的文件
            QFile out_file_process(file_name_wiht_path.replace(".log", "_PROCESS_.log",
                                                               Qt::CaseInsensitive));
            QFile out_file_received_message(file_name_wiht_path.replace("_PROCESS_.log", "_MESSAGE_.log",
                                                                        Qt::CaseInsensitive));

            // 两个文件以只写、前文清空模式打开
            if (out_file_process.open(QIODevice::WriteOnly | QIODevice::Truncate) &&
                            out_file_received_message.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
                QTextStream out_stream_process(&out_file_process);
                QTextStream out_stream_message(&out_file_received_message);

                QString content;
                // 按行读取数据，分别保存到2个文件中
                while (0 != (content = in.readLine()).length()) {
                    if (content.contains("sysRecv 1 received message")) {
                        out_stream_message<< content.replace("sysRecv 1 received message", "") << Qt::endl;
                    } else if (content.contains("sysRecv 1 process")) {
                        out_stream_process<< content.replace("sysRecv 1 process ", "") << Qt::endl;
                    }
                }
            }
        }
    }
}

void File_process::sl_read_data_from_file(QStringList file_list)
{
    QString file_name_wiht_path("");

    // 遍历文件名列表
    foreach(file_name_wiht_path, file_list) {
        QFile file(file_name_wiht_path);

        // 文件不存在
        if ( !file.exists()) {
            continue;
        }

        if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
            QTextStream in(&file);
            in.setAutoDetectUnicode(true);

            QString content("");

            // 按行读取文件内容，处理文件数据
            while ( 0 != (content = in.readLine()).length()) {
                emit si_send_data_to_model(data_process_->get_track_data(content));
            }
        }
    }
}

void File_process::sl_fetch_filter_conditions(QMap<QString, QString> map)
{
    Q_UNUSED(map)
}
